package com.yanqu.road.server.manager.match;

import com.yanqu.road.entity.config.match.MatchInfo;
import com.yanqu.road.entity.config.match.MatchReward;
import com.yanqu.road.logic.bussiness.match.MatchBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author ：zfr
 * @date ：2021/2/4 10:30
 * 赛事配置
 */
public class CrossMatchConfigMgr extends TempMgr {

    public static Map<Integer, Integer> matchLevelNumMap;
    /**
     * 杂艺基本配置 level-list
     */
    public static Map<Integer, List<MatchInfo>> matchListMap;
    /**
     * 杂艺奖励配置 type-list
     */
    public static Map<Integer, List<MatchReward>> matchRewardsMap;

    public static int VarietyHighLevel = 3;

    @Override
    public boolean reloadConfig() throws Exception {
        matchLevelNumMap = new ConcurrentHashMap<>();
        String[] param = GameConfig.VARIETY_OPEN_NUM.split(";");
        for (int i = 0; i < param.length; i++) {
            int matchNum = Integer.parseInt(param[i]);
            matchLevelNumMap.put(i + 1, matchNum);
        }
        matchListMap = MatchBussiness.getMatchListMap();
        matchRewardsMap = MatchBussiness.getMatchRewardsMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        matchLevelNumMap = new ConcurrentHashMap<>();
        matchListMap = new ConcurrentHashMap<>();
        matchRewardsMap = new ConcurrentHashMap<>();
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 随机杂艺
     *
     * @param matchMap
     * @param level 等级
     * @return MatchInfo
     */
    public static MatchInfo randomMatch(Map<Integer, Match> matchMap, int level, boolean isAllOccupation) {
        //根据等级取出杂艺配置集合
        List<MatchInfo> matchInfos = matchListMap.get(level);
        List<MatchInfo> randomMatchInfos = new ArrayList<>();
        Map<Integer, Match> openMap = new ConcurrentHashMap<>();
        for (Match match : matchMap.values()) {
            openMap.put(match.getMatchData().getMatchId(), match);
        }
        for (MatchInfo matchInfo : matchInfos) {
            if (!openMap.containsKey(matchInfo.getId())) {
                randomMatchInfos.add(matchInfo);
            }
        }
        if (isAllOccupation) {
            randomMatchInfos.removeIf(matchInfo -> matchInfo.getOccupation() != 0);
        } else {
            randomMatchInfos.removeIf(matchInfo -> matchInfo.getOccupation() == 0);
        }

        if (randomMatchInfos.size() <= 0) {
            return null;
        }
        List<Integer> weights = new ArrayList<>();
        for (MatchInfo matchInfo : randomMatchInfos) {
            weights.add(matchInfo.getWeight());
        }
        //随机下标
        int index = RandomHelper.getRandomIndexByWeight(weights, new Random());
        return randomMatchInfos.get(index);
    }

    public static MatchInfo getMatchInfo(int level, int matchId) {
        List<MatchInfo> matchInfos = matchListMap.get(level);
        if (matchInfos != null) {
            for (MatchInfo matchInfo : matchInfos) {
                if (matchInfo.getId() == matchId) {
                    return matchInfo;
                }
            }
        }
        return null;
    }

    /**
     * 是否是获取前一天的赛事
     * @return
     */
    public static boolean isGetLastDayMatch() {
        long zeroTime = DateHelper.getTodayZeroTimeStamp();
        long nowTime = System.currentTimeMillis();
        return nowTime < (zeroTime + GameConfig.VARIETY_BEGIN_TIME * 60 * 1000);
    }

    /**
     * 根据排行取奖励
     * @param level
     * @param matchId
     * @param index
     * @return
     */
    public static MatchReward getReward(int level, int matchId, int index) {
        int rewardsType = 0;
        MatchInfo matchInfo = getMatchInfo(level, matchId);
        if (matchInfo != null) {
            rewardsType = matchInfo.getRewardsType();
        }
        List<MatchReward> matchRewards = matchRewardsMap.get(rewardsType);
        if (matchRewards != null) {
            for (MatchReward matchReward : matchRewards) {
                if (index >= matchReward.getParam().get(0) && (index <= matchReward.getParam().get(1) || matchReward.getParam().get(1) == -1)) {
                    return matchReward;
                }
            }
        }
        return null;
    }
}
